package top.hencter.weibo.adapter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import top.hencter.weibo.R;
import top.hencter.weibo.bean.IndexItem;
import top.hencter.weibo.bean.VideoItem;

public class VideoListAdapter extends ArrayAdapter<VideoItem> {

    private int resourceId;
    private Context mContext;
    public VideoListAdapter(Context context, int resource, List<VideoItem> objects) {
        super(context, resource, objects);
        resourceId = resource;
        mContext = context;
    }


    @Override
    public View getView(int position, View convertView,  ViewGroup parent) {
        View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
        VideoItem cardContent = getItem(position);

        // 渲染用户头像
        ImageView avatarView = view.findViewById(R.id.index_avatar);
        Glide.with(getContext()).load(cardContent.getAvatar()).into(avatarView); // 链式调用

        // 渲染用户名
        TextView titleView = view.findViewById(R.id.post_author);
        titleView.setText(cardContent.getName());
        // 渲染博文日期
        TextView dataView = view.findViewById(R.id.post_time);
        dataView.setText(cardContent.getDate());
        // 渲染博文
        TextView contentView = view.findViewById(R.id.content);
        contentView.setText(cardContent.getContent());

//        // 渲染内容图片
//        GridView gridView = view.findViewById(R.id.index_image_grid_view);
//        IndexCardImageAdapter adapter = new IndexCardImageAdapter(getContext(), R.layout.index_gridview_item, cardContent.getImages());
//        gridView.setAdapter(adapter);

        // 渲染分享数
        TextView shareCountView = view.findViewById(R.id.share_count);
        shareCountView.setText(cardContent.getShares()+"");

        // 渲染评论数
        TextView commentCountView = view.findViewById(R.id.comment_count);
        commentCountView.setText(cardContent.getComments()+"");

        // 渲染点赞数
        TextView likeCountView = view.findViewById(R.id.like_count);
        likeCountView.setText(cardContent.getLikes()+"");

        // 播放控件
        ImageView playControl = view.findViewById(R.id.play);
        playControl.setVisibility(View.INVISIBLE); // 设置其不可见

        // 视频
        VideoView videoView = view.findViewById(R.id.video);
        Uri uri = Uri.parse(cardContent.getVideoUrl());
        videoView.setVideoURI(uri);
        videoView.start();

        videoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (videoView.isPlaying()) {
                    videoView.pause();
                    playControl.setVisibility(View.VISIBLE);
                } else {
                    videoView.start();
                    playControl.setVisibility(View.INVISIBLE);
                }
            }
        });

        return view;
    }
}
